package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Order;
import com.atguigu.service.OrderService;
import com.atguigu.utils.RedisMessageConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * OrderController
 *
 * @Author: 马伟奇
 * @CreateTime: 2021-02-02
 * @Description:
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("/findById")
    public Result findById(Integer id){
      Map map = orderService.findById(id);
      return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,map);
    }

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        try {
            // 获取用户输入的手机号码
            String validateCode = (String) map.get("validateCode");
            // 获取用户填写的手机号码
            String telephone = (String) map.get("telephone");

            // 取出redis里面的验证码
            String codeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
            // 校验用户输入的手机号码是否正确
            if (codeInRedis==null || !validateCode.equals(codeInRedis)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }

            // 设置订单类型
            map.put("orderType", Order.ORDERTYPE_WEIXIN);

            Result result =  orderService.add(map);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}